package org.example.dao;

import org.example.systemset_pojo.Employee;

import java.sql.*;

public class EmployeeDao {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/restaurant_management";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "password";

    // 根据工号查找员工
    public Employee findByJobNo(String jobNo) {
        Employee employee = null;
        String sql = "SELECT employee_id, name, phone, job_no, password, store_id, join_date, status " +
                "FROM employee WHERE job_no = ?";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, jobNo);
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                employee = new Employee();
                employee.setEmployeeId(rs.getInt("employee_id"));
                employee.setName(rs.getString("name"));
                employee.setPhone(rs.getString("phone"));
                employee.setJobNo(rs.getString("job_no"));
                employee.setPassword(rs.getString("password"));
                employee.setStoreId(rs.getInt("store_id"));
                employee.setJoinDate(rs.getDate("join_date"));
                employee.setStatus(rs.getString("status"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return employee;
    }

    // 根据用户名（姓名）查找员工
    public Employee findByName(String name) {
        Employee employee = null;
        String sql = "SELECT employee_id, name, phone, job_no, password, store_id, join_date, status " +
                "FROM employee WHERE name = ?";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, name);
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                employee = new Employee();
                employee.setEmployeeId(rs.getInt("employee_id"));
                employee.setName(rs.getString("name"));
                employee.setPhone(rs.getString("phone"));
                employee.setJobNo(rs.getString("job_no"));
                employee.setPassword(rs.getString("password"));
                employee.setStoreId(rs.getInt("store_id"));
                employee.setJoinDate(rs.getDate("join_date"));
                employee.setStatus(rs.getString("status"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return employee;
    }
}
